import { EventKeyboard, KeyCode, Node } from "cc";
import { camera } from "../canvas/camera/camera";

export default class Move {

  /**
   * 
   * @param NodeEventType 键盘监听
   * @param element 节点
   * @param position 节点位置
   * @param speed 移动速度
   */
  static humanMove(NodeEventType: EventKeyboard, element: Node, position: any, speed: number) {
    switch (NodeEventType.keyCode) {
      case KeyCode.KEY_A:
        camera.state_human_face_temp = 'player_left'
        element.setPosition((position.x -= speed), position.y)
        break;
      case KeyCode.KEY_D:
        camera.state_human_face_temp = 'player_right'
        element.setPosition((position.x += speed), position.y)
        break;
      case KeyCode.KEY_W:
        camera.state_human_face_temp = 'player_up'
        element.setPosition(position.x, (position.y += speed))
        break;
      case KeyCode.KEY_S:
        camera.state_human_face_temp = 'player_down'
        element.setPosition(position.x, (position.y -= speed))
        break;

    }
  }
  /**
   * 
   * @param NodeEventType 键盘监听
   * @param element 节点
   * @param position 节点位置
   * @param speed 移动速度
   * @param angSize 旋转角度
   */
  static tankMove(NodeEventType: EventKeyboard, element: Node, position: any, speed: number, angSize: number): void {
    const ang = element.angle//角度
    // console.log(ang)
    switch (NodeEventType.keyCode) {
      case KeyCode.KEY_A:
        //初始化位置旋转度数，方便从360到270，而不是0到270
        if (ang == 0) element.angle = 360
        //因为是2d图片，所以我们用angle控制y即可，但是3D的话我们要考虑x,y,z，使用的是setPosition
        if (ang == 90) {
          //如果向反方向，则不作行为，或者行为速度变慢
        }
        //从上往左   W->A
        else if (ang >= 180 && ang < 270) {
          element.angle += angSize
          speed = 0
        }
        //从下往左  到270终止,270方向即向左 S->A
        else if (ang > 270 && ang <= 360) {
          element.angle -= angSize
          speed = 0
        }
        else if (ang == 270) {
          element.setPosition((position.x - speed), position.y)
        };
        break;
      case KeyCode.KEY_D:
        //初始化位置旋转度数，方便从0到90，而不是360到90
        if (ang == 360) element.angle = 0
        if (ang == 270) {
          //如果向反方向，则不作行为，或者行为速度变慢
        }
        //到90终止
        // D->S
        else if (ang < 90 && ang >= 0) {
          element.angle += angSize
          speed = 0
        }
        // D->W
        else if (ang <= 180 && ang > 90) {
          element.angle -= angSize
          speed = 0
        }
        else if (ang == 90) {
          element.setPosition((position.x + speed), position.y)
        };
        break;
      case KeyCode.KEY_W:
        if (ang == 360 || ang == 0) { }
        // D->W
        else if (ang >= 90 && ang < 180) {
          element.angle += angSize
          speed = 0
        }
        // A->W
        else if (ang <= 270 && ang > 180) {
          element.angle -= angSize
          speed = 0
        }
        else if (ang == 180) { element.setPosition(position.x, (position.y + speed)) };
        break;
      case KeyCode.KEY_S:
        if (ang == 180) { }
        // A->S
        else if (ang >= 270 && ang < 360) {
          element.angle += angSize //迈向360度情况
          speed = 0
        }
        // D->S 
        else if (ang > 0 && ang <= 90) {
          element.angle -= angSize //迈向0度情况
          speed = 0
        }
        else if (ang == 0 || ang == 360) { element.setPosition(position.x, (position.y - speed)) };
        break;
    }
  }
}
